package com.dingqb.myapplication;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    ImageView iv_oval1;
    ImageView iv_oval2;
    ImageView iv_oval3;
    ImageView iv_oval4;
    Button btn_begin;
    ImageView iv_frame;
    ImageView iv_translate;

    void initView() {
        iv_oval1 = findViewById(R.id.iv_oval1);
        iv_oval2 = findViewById(R.id.iv_oval2);
        iv_oval3 = findViewById(R.id.iv_oval3);
        iv_oval4 = findViewById(R.id.iv_oval4);
        btn_begin = findViewById(R.id.btn_begin);
        iv_frame = findViewById(R.id.iv_frame);
        iv_translate = findViewById(R.id.iv_translate);
        iv_oval1.setOnClickListener(this);
        iv_oval2.setOnClickListener(this);
        iv_oval3.setOnClickListener(this);
        iv_oval4.setOnClickListener(this);
        btn_begin.setOnClickListener(this);
        iv_frame.setOnClickListener(this);
        iv_translate.setOnClickListener(this);
    }

    void setAnim() {
        Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_scale);
        Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_scale);
        Animation animation3 = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_scale);
        Animation animation4 = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_scale);
        animation1.setStartOffset(500);
        animation2.setStartOffset(1000);
        animation3.setStartOffset(1500);
        animation4.setStartOffset(2000);
        iv_oval1.startAnimation(animation1);
        iv_oval2.startAnimation(animation2);
        iv_oval3.startAnimation(animation3);
        iv_oval4.startAnimation(animation4);
    }

    /**
     *
     */
    void setFrame_() {
        /**
         * defType:"drawable"、"string"、"array" "color"
         */
        int id1 = getResources().getIdentifier("qq", "drawable", getPackageName());
        int id2 = getResources().getIdentifier("wechat", "drawable", getPackageName());
        int id3 = getResources().getIdentifier("ic_launcher", "mipmap", getPackageName());
        Drawable d1 = getResources().getDrawable(id1);
        Drawable d2 = getResources().getDrawable(id2);
        Drawable d3 = getResources().getDrawable(id3);
        AnimationDrawable animationDrawable = new AnimationDrawable();
        animationDrawable.addFrame(d1, 200);
        animationDrawable.addFrame(d2, 200);
        animationDrawable.addFrame(d3, 200);
        animationDrawable.setOneShot(false);
        iv_frame.setBackground(animationDrawable);
        animationDrawable.start();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {

            case R.id.iv_oval1:
            case R.id.iv_oval2:
            case R.id.iv_oval3:
            case R.id.iv_oval4:
                setAnim();
                break;
            case R.id.btn_begin:
                setAnim();
                break;
            case R.id.iv_frame:
                setFrame_();
                break;
            case R.id.iv_translate:

                Animation animation_translate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
                iv_translate.startAnimation(animation_translate);
                break;
        }
    }
}
